/***********************************************************************************
Copy right:	    Coffee Tech.
Author:         jiaoyue
Date:           2021-12-01
Description:    mqtt通信封装
***********************************************************************************/
#include "mqtt_handler.h"
#include "msg_handler.h"

//#define ADDRESS     "tcp://192.168.1.118:1883"
//#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

static MQTTClient_deliveryToken token;
static MQTTClient client;

void delivered(void *context, MQTTClient_deliveryToken dt)
{
    // printf("Message with token value %d delivery confirmed\n", dt);
    token = dt;
}

int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
//    LOGD("Message arrived");
//    LOGD("    topic:%s", topicName);
//    LOGD("    message:%s", message->payload);
    msg_handle(topicName, message->payload);

    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}

void connlost(void *context, char *cause)
{
    printf("\nConnection lost\n");
    printf("     cause: %s\n", cause);
}

int mqtt_publish(const char *topic, char *msg)
{
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    pubmsg.payload = msg;
    pubmsg.payloadlen = (int)strlen(msg);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;    
    return MQTTClient_publishMessage(client, topic, &pubmsg, &token);
}

int mqtt_init()
{
    //先解析出mqtt的地址和端口
    char mqtt_addr[256] = {0};
    char addr[64] = {0};
    int mqtt_port;

    struct list_head nv_list;
    nv_get_list_from_file(&nv_list, NODE_CONFIG_PAHT);

    struct nv_obj *pobj;
    pobj = nv_get_list_obj(&nv_list, "mqtt_server.addr");
    if(NULL == pobj)
    {
        LOG_STD(APP_ERR, "err get mqtt addr");
        exit(-1);
    }
    strcpy(addr, pobj->valuestring);

    pobj = nv_get_list_obj(&nv_list, "mqtt_server.port");
    if(NULL == pobj)
    {
        LOG_STD(APP_ERR, "err get mqtt port");
        exit(-1);
    }
    mqtt_port = pobj->valueint;

    sprintf(mqtt_addr, "tcp://%s:%d", addr, mqtt_port);
    LOG_STD(APP_NOTICE, "mqtt_addr = %s", mqtt_addr);

    //nv_get_list和nv_list_free成对存在
    nv_list_free(&nv_list);

    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    int rc;

    MQTTClient_create(&client, mqtt_addr, MODULE, MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1; 

    MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered);

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        LOG_STD(APP_ERR, "Failed to connect, return code %d\n", rc);
        return -1;
    }
    LOG_STD(APP_NOTICE, "mqtt connect success");

     MQTTClient_subscribe(client, TOPIC_DATA_DOWN, QOS);
     MQTTClient_subscribe(client, TOPIC_CTL_DOWN, QOS);

    return 0;
}
